package com.py.framework.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.py.framework.core.common.Page;
import com.py.framework.core.exception.ServiceException;
import com.py.framework.core.rest.support.RestServiceResult;
import com.py.framework.service.TestService;

@Controller
@RequestMapping("/test")
public class TestController {
	@Autowired
	private TestService testService;
	
	/**
	 * Test 
	 * @param username
	 * @return  {"code":0,"msg":null,"count":0,"data":[{"resultInfo":"lee"}]}
	 */
	@RequestMapping(value = "/getTestInfo", method = RequestMethod.GET)
	public @ResponseBody RestServiceResult<List<Object>> getTestInfo(String username) {
		RestServiceResult<List<Object>> result = new RestServiceResult<>();
		
		List<Object> bigItem = new ArrayList<>();
		Map<String, Object> item = new HashMap<>();
		String data = testService.getTestInfo(username);
		item.put("resultInfo", data);
		bigItem.add(item);
		
		result.setData(bigItem);
		return result;
	}
	
	/**
	 * Test Page
	 * @param username
	 * @return  {"code":0,"msg":null,"count":0,"data":[{"resultInfo":"lee"}{"resultInfo":"lancy"}]}
	 */
	@RequestMapping(value = "/queryInfoForPage", method = RequestMethod.GET)
	public @ResponseBody RestServiceResult<List<Map<String, Object>>> queryInfoForPage(String username) {
		RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<>();
		
		Page page = new Page();
		page.setCurrentPage(1);
		page.setPageSize(20);;
		List<Map<String, Object>> datas = testService.queryInfoForPage(username, page);
		
		result.setCount(page.getTotalCount());
		result.setData(datas);
		return result;
	}
	
	/**
	 * Test
	 * @param username
	 * @return  {"code":-1,"msg":"传入的参数为空！","count":0,"data":{}}
	 */
	@RequestMapping(value = "/testExInfo", method = RequestMethod.GET)
	public @ResponseBody RestServiceResult<List<Object>> testExInfo(String username) {
		RestServiceResult<List<Object>> result = new RestServiceResult<>();
		if (StringUtils.isBlank(username)) {
			throw new ServiceException("传入的参数为空！");
		}
		
		List<Object> bigItem = new ArrayList<>();
		Map<String, Object> item = new HashMap<>();
		String data = testService.getTestInfo(username);
		item.put("resultInfo", data);
		bigItem.add(item);
		
		result.setData(bigItem);
		return result;
	}
}
